package com.ssyshop.info.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

import com.ssyshop.info.R;
import com.ssyshop.info.https.OkHttpClientManager;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.FormBody;
import okhttp3.Response;


public class Config {


    //服务器URL
//    public static String URL_SERVER = "http://192.168.3.18/";
    public static String URL_SERVER = "https://www.ssyshop.com/";
    public static final String APP_KEY="5E523B95-410C-44A2-8298-409F2C1A20B4";

    public static double newVerCode = 0;
    public static String newVerName;
    public static String UpDate_URL;// 下载地址

    //广播常量
    public static final String BROADCAST_LOAD="com.broadcast.load";//监听有新版本的"以后再说"按钮

    //其他常量

    /**
     * 获取当前版本号
     *
     * @param context
     * @return
     */
    public int getVerCode(Context context) {
        int verCode = -1;
        try {
            // 获取packagemanager的实例
            PackageManager packageManager = context.getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            verCode = packInfo.versionCode;

        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return verCode;
    }

    /**
     * 获取版本名称
     *
     * @param context
     * @return
     */
    public String getVerName(Context context) {
        String verName = "";

        try {
            // 获取packagemanager的实例
            PackageManager packageManager = context.getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            verName = packInfo.versionName;

        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }

    public static String getAppName(Context context) {
        String verName = context.getResources().getText(R.string.app_name).toString();
        return verName;
    }

    /**
     * 检查服务器的版本
     *
     * @return
     */
    public double getServerVerCode() {
        try {

           Log.i("","获取服务端");

            HashMap<String, String> map = updateVersion();
            if (map == null) {
                return newVerCode;
            }
            newVerCode = Double.parseDouble(map.get("verCode"));
            UpDate_URL = map.get("downloadUrl").toString();
            newVerName = map.get("verName").toString();
            Log.i("downUrl", UpDate_URL);
            Log.i("Name", newVerName);
            System.out.println("newVerCode--->>>" + map.get("verCode"));

        } catch (Exception e) {
            System.out.println(e);
            return newVerCode;
        }
        return newVerCode;
    }

    /**
     * 客户端版本更新
     *
     * @param
     * @return
     */
    public HashMap<String, String> updateVersion() {
        HashMap<String, String> map = new HashMap<String, String>();
        String str = "";//从接口获取更新数据
        str=OkHttpClientManager.sendGet(URL_SERVER + "api.php?app_key=" + APP_KEY + "&method=ssy.base.app.set");
        if (str != null) {// 相应数据不为空
            try {
                // 解决UTF-8 另存文件是BOM打头造成解析JSON错误的问题
                if (str != null && str.startsWith("\ufeff")) {
                    str = str.substring(1);
                }
                JSONObject object = new JSONObject(str);
                JSONObject object1=object.getJSONObject("info");
                map.put("verCode", object1.getString("app_set_ver"));// 版本号
                map.put("downloadUrl", object1.getString("app_update_url"));// 版本地址
                map.put("verName", object1.getString("app_update_remarks"));// 版本名称
                return map;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }



}
